package com.example.asus_pc.myshop.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.example.asus_pc.myshop.R;
import com.example.asus_pc.myshop.adapter.EditVegaAdatpter;
import com.example.asus_pc.myshop.adapter.ShowVegaAdatpter;
import com.example.asus_pc.myshop.bean.VegaBean;
import com.example.asus_pc.myshop.utils.Cn2Spell;
import com.example.asus_pc.myshop.utils.SharedPreUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class EditVegasActivity extends AppCompatActivity implements View.OnClickListener{


    //    @BindView(R.id.tv_right)
    TextView tvRight;

    TextView tvAdd;
    //    @BindView(R.id.rv_edit)
    RecyclerView rvEdit;

    RecyclerView rvShow;
    private EditVegaAdatpter editAdatpter;
    private ShowVegaAdatpter showAdatpter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_vegas);
//        ButterKnife.bind(this);
        initViews();

        setListeners();


    }



    private void initViews() {
        tvAdd = (TextView) findViewById(R.id.tv_add);
        tvAdd.setVisibility(View.GONE);
        rvEdit = (RecyclerView) findViewById(R.id.rv_edit);
        tvRight = (TextView) findViewById(R.id.tv_right);
        rvShow = (RecyclerView) findViewById(R.id.rv_show);

        rvEdit.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        editAdatpter = new EditVegaAdatpter(this);
        rvEdit.setAdapter(editAdatpter);

        rvShow.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        showAdatpter = new ShowVegaAdatpter(this);
        rvShow.setAdapter(showAdatpter);
    }


    private void setListeners() {
        tvAdd.setOnClickListener(this);
        tvRight.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        String vegas = SharedPreUtils.getString(SharedPreUtils.VEGA_CONTENT, "");
        if(!vegas.equals("")){
            List<VegaBean> vegaBeans = VegaBean.jsonToList(vegas);
            showAdatpter.setDatas(vegaBeans);
            editAdatpter.setDatas(vegaBeans);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_right:
                if (tvRight.getText().toString().equals("编辑")){
                    tvRight.setText("完成");
                    tvAdd.setVisibility(View.VISIBLE);
                    rvEdit.setVisibility(View.VISIBLE);
                    rvShow.setVisibility(View.GONE);
                }else {
                    List<VegaBean> editDatas = editAdatpter.getDatas();
                    List<VegaBean> showDatas = new ArrayList<>();

                    tvRight.setText("编辑");
//                    StringBuilder sb = new StringBuilder();
                    String vegas = "";

                    if(editDatas.size()>1) {
                        for (int i = 0; i < editDatas.size(); i++) {
                            VegaBean bean = editDatas.get(i);
                            if(bean.getVegaName().equals("")){
                                continue;
                            }
                            bean.setVegaSpell(Cn2Spell.converterToSpell(bean.getVegaName()));
                            showDatas.add(bean);
//                            sb.append(bean.getVegaName()).append("#").append(bean.getTotalPrice()).append("#").append(bean.getSplitPrice());
//                            if(i!=editDatas.size()-2&&!editDatas.get(i+1).getVegaName().equals("")){
//                                sb.append("_");
//                            }
                        }
                        Collections.sort(showDatas,new VegaBean.VegaBeanOrder());
                        vegas = VegaBean.listToJson(showDatas);
                    }
                    SharedPreUtils.putString(SharedPreUtils.VEGA_CONTENT, vegas);
                    rvEdit.setVisibility(View.GONE);
                    rvShow.setVisibility(View.VISIBLE);
                    showAdatpter.setDatas(showDatas);
                    editAdatpter.setDatas(showDatas);
                    tvAdd.setVisibility(View.GONE);
//                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//                    imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                }
                break;
            case R.id.tv_add:
                editAdatpter.addMore();
                break;
        }
    }
}
